<template>
	<view class="cart">
		<view class="list" v-if="list.length">
			<checkbox-group @change="handleChecked">
				<view class="item" v-for="item in list" :key="item.id">
					<view class="top">
						<checkbox :checked="item.isSelected" :value="item.id" color="#435950" />
						<image :src="item.productPic" mode=""></image>
						<view class="info">
							<view class="name">{{ item.productName }}</view>
							<view class="skuId">编号: {{ item.productSkuId }}</view>
							<view class="price">价格: ￥{{ item.price }}</view>
							<view class="num">数量: {{ item.quantity }}</view>
						</view>
					</view>
					<view class="bottom">
						<view class="stepper">
							<view class="subtract" @tap="handleSub(item)">-</view>
							<view class="num">{{ item.quantity }}</view>
							<view class="add" @tap="handleAdd(item)">+</view>
						</view>
					</view>
				</view>
			</checkbox-group>
		</view>
		<empty v-else></empty>
		<view class="footer">
			<label>
				<checkbox :checked="selectAll" @tap="handleCheckedAll" color="#435950" />
				<text>全选</text>
			</label>
			<view class="btns">
				<view class="total">
					合计:￥
					<text>{{ total.toFixed(2) }}</text>
				</view>
				<view class="clearingBtn" @tap="handleClearing()">结算</view>
			</view>
		</view>
	</view>
</template>

<script>
import { findAllCartItem, delCartItems } from '../../api/cart/index.js';
import { addPreOrder } from '../../api/kind/productDetail/index.js';
import mixin from '../../mixins/mixin.js';
export default {
	mixins: [mixin],
	data() {
		return {
			list: [],
			selectAll: false, // 是否全选
			total: 0 // 总计
		};
	},
	methods: {
		// 获取购物车所有商品
		async getCartList() {
			const { success, data } = await findAllCartItem();
			if (success) {
				this.list = data.items.map(el => {
					return {
						...el,
						isSelected: false
					};
				});
			} else {
				uni.showToast({
					title: '网络错误,请稍后再试',
					icon: 'error'
				});
			}
		},
		// 删除购物车商品
		async deleteItem(id) {
			const { success, message } = await delCartItems([id]);
			if (success) {
				uni.showToast({
					title: '删除成功'
				});
				// this.getCartList();
				// 删除本地,如果重新获取列表,被选中的item会被重置
				let index = this.list.findIndex(el => el.id == id);
				this.list.splice(index, 1);
			} else {
				uni.showToast({
					title: message
				});
			}
		},
		// 减少购买数量
		handleSub(item) {
			if (item.quantity == 1) {
				uni.showModal({
					title: '确定要删除该商品吗?',
					success: ({ confirm }) => {
						if (!confirm) {
							return;
						}
						this.deleteItem(item.id);
					}
				});
			} else {
				item.quantity--;
			}
			this.setTotal();
		},
		// 增加购买数量
		handleAdd(item) {
			item.quantity++;
			this.setTotal();
		},
		// checkbox change
		handleChecked(e) {
			console.log(e);
			let selected = e.detail.value;
			this.list.forEach(el => {
				if (selected.includes(el.id)) {
					el.isSelected = true;
				} else {
					el.isSelected = false;
				}
			});
			// 全选
			if (e.detail.value.length == this.list.length) {
				this.selectAll = true;
			} else {
				this.selectAll = false;
			}
			this.setTotal();
		},
		// 全选点击
		handleCheckedAll() {
			this.selectAll = !this.selectAll;
			this.list.forEach(el => {
				el.isSelected = this.selectAll;
			});
			this.setTotal();
		},
		// 计算总计
		setTotal() {
			this.total = this.list.reduce((sum, next) => {
				if (next.isSelected) {
					return (sum += next.price * next.quantity);
				} else {
					return sum;
				}
			}, 0);
		},
		// 结算
		async handleClearing() {
			// 没有商品直接return
			if (!this.list.length) {
				return;
			}
			let orderItemList = this.list
				.filter(el => {
					return el.isSelected;
				})
				.map(item => {
					return {
						cartId: item.id,
						productId: item.productId,
						productQuantity: item.quantity,
						productSkuId: item.productSkuId
					};
				});
			// 没有勾选商品return
			if (!orderItemList.length) {
				uni.showToast({
					title: '您没购买任何商品!',
					icon: 'none'
				});
				return;
			}
			const {
				success,
				message,
				data: { orderId }
			} = await addPreOrder({ orderItemList });
			if (success) {
				uni.navigateTo({
					url: `../order/confirmorder/confirmorder?id=${orderId}`
				});
			} else {
				uni.showToast({
					title: message,
					icon: 'error'
				});
			}
		}
	},
	onLoad() {
		if (this.checkLogin()) {
			this.getCartList();
		}
	}
};
</script>

<style lang="scss" scoped>
.cart {
	overflow: hidden;
	// 去掉checkbox获得焦点后蓝色边框
	/deep/ .uni-checkbox-input:hover {
		border-color: #d1d1d1;
	}
	// item
	.list {
		padding: 0 30rpx;
		height: calc(100vh - 194rpx);
		overflow: auto;
		.item {
			margin: 50rpx auto;
			background-color: #fff;
			border-radius: 24rpx;
			padding: 20rpx;
			box-sizing: border-box;
			.top {
				display: flex;
				align-items: center;
				image {
					width: 136rpx;
					height: 136rpx;
					margin: 0 20rpx;
				}
				.info {
					flex: 1;
					color: #8d8d8d;
					font-size: 22rpx;
					.name {
						font-size: 24rpx;
						color: #3e3e3e;
					}
				}
			}
			.bottom {
				display: flex;
				justify-content: flex-end;
				.stepper {
					display: flex;
					text-align: center;
					line-height: 60rpx;
					.subtract,
					.num,
					.add {
						height: 60rpx;
						width: 60rpx;
						border: none;
						background: #f1ece7;
						border-radius: 42rpx;
						margin: 0 4rpx;
					}
					.num {
						width: 90rpx;
					}
				}
			}
		}
	}
	// 底部
	.footer {
		display: flex;
		justify-content: space-between;
		position: fixed;
		bottom: 0;
		width: 100%;
		box-sizing: border-box;
		height: 150rpx;
		padding: 0 40rpx;
		align-items: center;
		background: #fff;
		.btns {
			display: flex;
			align-items: center;
			.total text {
				color: #f00;
			}
			.clearingBtn {
				width: 208rpx;
				height: 80rpx;
				background: #354e44;
				border-radius: 28rpx;
				color: #fff;
				font-size: 36rpx;
				line-height: 80rpx;
				text-align: center;
				margin-left: 10rpx;
			}
		}
	}
}
</style>
